package com.crm.action;

import com.crm.service.IUserService;
import com.crm.model.User;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version 1.0
 */
public class Login extends JSONController{

    private static final Logger LOGGER = Logger.getLogger(Login.class);

    private IUserService userServ;
    private String formView;
    private String successView;

    public void setUserService(IUserService userServ) {
        this.userServ = userServ;
    }

    protected JSONObject handleAjaxRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject json = new JSONObject();
        String uname = request.getParameter("username");
        String pwd = request.getParameter("password");
        User user = userServ.getUserByNameAndPwd(uname, pwd);
        if (user != null) {
            json.put("success",true).put("msg","ok");
        } else {
            json.put("success",true).put("msg","帐号或密码错误！");
        }

        return json;
    }

//    public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {
//        String u = req.getParameter("username");
//        String p = req.getParameter("password");
//        if (null == u || null == p || "".equals(u) || "".equals(p)) {
//            return new ModelAndView(formView);
//        }
//        User user = userServ.getUserByNameAndPwd(u, p);
//        if (user != null) {
//            return new ModelAndView(successView);
//        } else {
//            return new ModelAndView(formView);
//        }
//    }

    public void setFormView(String formView) {
        this.formView = formView;
    }

    public void setSuccessView(String successView) {
        this.successView = successView;
    }
}
